package com.shop2cn.iapi.shenqisettlement.testcase;

import java.math.BigDecimal;
import java.util.HashMap;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.shenqisettlement.parameter.GetUserBandingBankCardInfoReq;
import com.shop2cn.iapi.shenqisettlement.parameter.resp.GetUserBandingBankCardInfoResp;
import com.shop2cn.iapi.shenqisettlement.service.GetUserBandingBankCardInfoCall;
import com.ymatou.common.lang.CipherUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.database.model.shenqisettlement.CompanyInfo;
import com.ymttest.database.model.shenqisettlement.TeFeeRateConfig;
import com.ymttest.database.model.shenqisettlement.TeWithdrawFeeConfig;
import com.ymttest.database.model.shenqisettlement.UserBankAccount;
import com.ymttest.database.sqlwapper.godmch.mch_infoWapper;
import com.ymttest.database.sqlwapper.shenqisettlement.CompanyInfoWrapper;
import com.ymttest.database.sqlwapper.shenqisettlement.TeFeeRateConfigWrapper;
import com.ymttest.database.sqlwapper.shenqisettlement.TeWithdrawFeeConfigWrapper;
import com.ymttest.database.sqlwapper.shenqisettlement.UserBankAccountWrapper;
import com.ymttest.utils.DataUtil;
import com.ymttest.utils.MapUtil;

public class Ts_GetUserBandingBankCardInfo {

	private static GetUserBandingBankCardInfoReq getUserBandingBankCardInfoReq = null;
	private static GetUserBandingBankCardInfoCall getUserBandingBankCardInfoCall = null;

	private static String key = "123456";

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取用户绑定的银行卡信息");
	}

	@Before
	public void caseUp() {

		getUserBandingBankCardInfoReq = new GetUserBandingBankCardInfoReq();
		getUserBandingBankCardInfoCall = new GetUserBandingBankCardInfoCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult("api/getUserBandingBankCardInfo");
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	public void verify() throws Exception {

		GetUserBandingBankCardInfoResp expectResp = getUserBandingBankCardInfoCall
				.getResult();

		long actualUserId = getUserBandingBankCardInfoReq.getUserId();
		String actualCurrency = new mch_infoWapper()
				.selectby(MapUtil.hashMap("mchId", actualUserId)).get(0)
				.getCurrency();

		Logger.verifyEquals(expectResp.getUserId(), actualUserId, "验证userId");

		UserBankAccountWrapper userBankAccountWrapper = new UserBankAccountWrapper();
		UserBankAccount actualBankAccount = userBankAccountWrapper
				.selectByUserIdAndType(actualUserId, getUserBandingBankCardInfoReq.getAccountType());

		HashMap<String, String> expectMap = expectResp
				.getBankRequiredFieldMap();

		for (HashMap.Entry<String, String> entry : expectMap.entrySet()) {

			if (entry.getKey().equalsIgnoreCase("bankName"))
				Logger.verifyEquals(entry.getValue(),
						actualBankAccount.getBank_name(), "验证bank_name");
			else if (entry.getKey().equalsIgnoreCase("bankBranch"))
				Logger.verifyEquals(entry.getValue(),
						actualBankAccount.getBank_branch(), "验证bank_branch");
			else if (entry.getKey().equalsIgnoreCase("bankSwiftCode"))
				Logger.verifyEquals(entry.getValue(),
						actualBankAccount.getBank_swift_code(),
						"验证bank_swift_code");
			else if (entry.getKey().equalsIgnoreCase("bankRoutingNumber"))
				Logger.verifyEquals(entry.getValue(),
						actualBankAccount.getBank_routing_number(),
						"验证bank_routing_number");
			else if (entry.getKey().equalsIgnoreCase("bankBsbCode"))
				Logger.verifyEquals(entry.getValue(),
						actualBankAccount.getBank_routing_number(),
						"验证bank_routing_number");
			else if (entry.getKey().equalsIgnoreCase("holerName"))
				Logger.verifyEquals(entry.getValue(),
						actualBankAccount.getHoler_name(), "验证holer_name");
			else if (entry.getKey().equalsIgnoreCase("country"))
				Logger.verifyEquals(entry.getValue(),
						actualBankAccount.getCountry(), "验证country");
			else if (entry.getKey().equalsIgnoreCase("holerDocNumber"))
				Logger.verifyEquals(
						entry.getValue(),
						CipherUtil.decryptAES(key,
								actualBankAccount.getHoler_doc_number()),
						"验证holer_doc_number");
			else if (entry.getKey().equalsIgnoreCase("holerContact"))
				Logger.verifyEquals(
						entry.getValue(),
						CipherUtil.decryptAES(key,
								actualBankAccount.getHoler_contact()),
						"验证holer_contact");
			else if (entry.getKey().equalsIgnoreCase("holerDob"))
				Logger.verifyEquals(entry.getValue(),
						actualBankAccount.getHoler_dob(), "验证holer_dob");
			else if (entry.getKey().equalsIgnoreCase("holerCountry"))
				Logger.verifyEquals(entry.getValue(),
						actualBankAccount.getHoler_country(), "验证holer_country");
			else if (entry.getKey().equalsIgnoreCase("holerAddress"))
				Logger.verifyEquals(entry.getValue(),
						actualBankAccount.getHoler_address(), "验证holer_address");
			else if (entry.getKey().equalsIgnoreCase("iban"))
				Logger.verifyEquals(entry.getValue(),
						actualBankAccount.getIban(), "验证iban");
		}

		Logger.verifyEquals(expectResp.getCountry(),
				actualBankAccount.getCountry(), "验证country");
		Logger.verifyEquals(expectResp.getCurrency(), actualCurrency,
				"验证currency");
		Logger.verifyEquals(expectResp.getAccountAuditStatus(),
				actualBankAccount.getAudit_status(), "验证accountAuditStatus");
		Logger.verifyEquals(expectResp.getAccountAuditFailReason(),
				actualBankAccount.getFail_reason(), "验证accountAuditFailReason");

		TeWithdrawFeeConfigWrapper teWithdrawFeeConfigWrapper = new TeWithdrawFeeConfigWrapper();
		TeWithdrawFeeConfig teWithdrawFeeConfig = teWithdrawFeeConfigWrapper
				.selectByCountry(expectResp.getCountry());

		DataUtil.verifyDecimalEquals(expectResp.getTeFeeUsd(),
				teWithdrawFeeConfig.getTransaction_fee(), "验证TeFeeUsd");
		DataUtil.verifyDecimalEquals(expectResp.getTeMaxWithdrawAmountUsd(),
				teWithdrawFeeConfig.getMax_size_fee(),
				"验证TeMaxWithdrawAmountUsd");

		TeFeeRateConfigWrapper teFeeRateConfigWrapper = new TeFeeRateConfigWrapper();
		TeFeeRateConfig teFeeRateConfig = teFeeRateConfigWrapper
				.selectByPrimaryKey(actualCurrency);
		
		int scale = 0;
		
		if (actualCurrency.equalsIgnoreCase("JPY"))
			scale = 0;
		else
			scale = 2;

		DataUtil.verifyDecimalEquals(
				expectResp.getMaxWithdrawAmount(),
				teFeeRateConfig.getRate().multiply(
						expectResp.getTeMaxWithdrawAmountUsd()),
				"验证maxWithdrawAmount");
		DataUtil.verifyDecimalEquals(expectResp.getFee(), teFeeRateConfig
				.getRate().multiply(expectResp.getTeFeeUsd()).setScale(scale, BigDecimal.ROUND_HALF_UP), "验证fee");

		if (!expectResp.getCountry().equalsIgnoreCase("CHN")) {

			CompanyInfoWrapper companyWrapper = new CompanyInfoWrapper();

			CompanyInfo actualCompany = companyWrapper
					.selectByUserIdAndAccountType(actualUserId, getUserBandingBankCardInfoReq.getAccountType());

			Logger.verifyEquals(
					expectResp.getCompanyAuditFailReason(),
					actualCompany.getFail_reason() == null ? "" : actualCompany
							.getFail_reason(), "验证CompanyAuditFailReason");
			Logger.verifyEquals(expectResp.getCompanyAuditStatus(),
					actualCompany.getCompany_status(), "验证CompanyAuditStatus");
			Logger.verifyEquals(expectResp.getCompanyName(),
					actualCompany.getCompany_name(), "验证CompanyName");
			Logger.verifyEquals(expectResp.getCompanyCode(),
					actualCompany.getCompany_code(), "验证CompanyCode");
			Logger.verifyEquals(expectResp.getCompanyLegalPerson(),
					actualCompany.getCompany_legal_person(),
					"验证CompanyLegalPerson");
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetUserBandingBankCardInfo_001() {
		Logger.start(true, "获取用户绑定的国内银行卡信息");
		try {

			getUserBandingBankCardInfoReq.setUserId(502216286);
			getUserBandingBankCardInfoReq.setAccountType(3);

			getUserBandingBankCardInfoCall
					.setData(getUserBandingBankCardInfoReq);
			getUserBandingBankCardInfoCall.callService();

			Logger.verifyEquals(true,
					getUserBandingBankCardInfoCall.isSuccess(), "验证success");
			Logger.verifyEquals(0,
					getUserBandingBankCardInfoCall.getRetultCode(),
					"验证resultCode");

			verify();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetUserBandingBankCardInfo_002() {
		Logger.start(true, "获取用户绑定的对公外币信息");
		try {

			getUserBandingBankCardInfoReq.setUserId(500002316);
			getUserBandingBankCardInfoReq.setAccountType(1);

			getUserBandingBankCardInfoCall
					.setData(getUserBandingBankCardInfoReq);
			getUserBandingBankCardInfoCall.callService();

			Logger.verifyEquals(true,
					getUserBandingBankCardInfoCall.isSuccess(), "验证success");
			Logger.verifyEquals(0,
					getUserBandingBankCardInfoCall.getRetultCode(),
					"验证resultCode");

			verify();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetUserBandingBankCardInfo_003() {
		Logger.start(true, "获取用户绑定的对私外币信息");
		try {

			getUserBandingBankCardInfoReq.setUserId(501361754);
			getUserBandingBankCardInfoReq.setAccountType(2);

			getUserBandingBankCardInfoCall
					.setData(getUserBandingBankCardInfoReq);
			getUserBandingBankCardInfoCall.callService();

			Logger.verifyEquals(true,
					getUserBandingBankCardInfoCall.isSuccess(), "验证success");
			Logger.verifyEquals(0,
					getUserBandingBankCardInfoCall.getRetultCode(),
					"验证resultCode");

			verify();

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetUserBandingBankCardInfo_004() {
		Logger.start(true, "userId为0");
		try {

			getUserBandingBankCardInfoReq.setUserId(0);
			getUserBandingBankCardInfoReq.setAccountType(2);

			getUserBandingBankCardInfoCall
					.setData(getUserBandingBankCardInfoReq);
			getUserBandingBankCardInfoCall.callService();

			Logger.verifyEquals(false,
					getUserBandingBankCardInfoCall.isSuccess(), "验证success");
			Logger.verifyEquals("userId:must be greater than or equal to 1",
					getUserBandingBankCardInfoCall.getResultMessage(),
					"验证resultMessage");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
